//
//  LTEllipseChartView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/22.
//

#import "LTEllipseChartView.h"
#import "Masonry.h"
#import "LTEllipseView.h"
static NSInteger baseLineCount = 5;//纵坐标个数
static NSInteger space = 37.5;//纵坐标个数
static NSInteger spaceX = 55;//纵坐标个数

@implementation LTEllipseChartView

{
    
    UILabel * titleLabel;//标题
    UIButton * arrowBtn;//标题箭头
    UILabel * unitLabel;//单位
    UILabel * desLabel1;//描述1
    UILabel * IdentifyLabel1;//颜色
    UILabel * desLabel2;//描述2
    UILabel * IdentifyLabel2;//颜色
    UILabel * desLabel3;//描述3
    UILabel * IdentifyLabel3;//颜色
    UIImageView * lastLineView;//最后一条背景线
    NSMutableArray * pointArray;//拐点数据
    NSMutableArray * xLabelArray;//横坐标
    NSMutableArray * btnArray;//按钮
    NSMutableArray * labelArray;//显示标签
    
    UIView *moveLine;
    
    CGPoint lastPoint;//最后一个坐标点
    
    CGFloat maxValue;//y轴最大值
}

-(void)setTitleStr:(NSString *)titleStr{
    
    _titleStr = titleStr;
    [arrowBtn setTitle:titleStr forState:UIControlStateNormal];
   
}

-(void)setUnitStr:(NSString *)unitStr{
    
    _unitStr = unitStr;
    unitLabel.text = _unitStr;
}

-(void)setType:(NSInteger)type{
    
    _type = type;
}

-(void)setArrayX:(NSMutableArray *)arrayX{
    
    _arrayX = arrayX;
}

-(void)setArrayY:(NSMutableArray *)arrayY{
    
    _arrayY = arrayY;
    
    maxValue = [[_arrayY valueForKeyPath:@"@max.floatValue"] floatValue];
    if (maxValue <= 1.00) {
        maxValue = 100;
    }else{
        maxValue = maxValue * 100;
       // maxValue = ceilf(maxValue);
       // int remainder = (int)maxValue%10;
        //确保maxValue能被8整除
       // maxValue = maxValue + (10 - remainder);
    }
    
    //背景线条 + Y轴
    [self initBaseLineViewWithArrayY:nil];
    //X轴
    [self initXLineWithArrayX:_arrayX];
    self.bgScrollView.contentSize = CGSizeMake( (_arrayX.count)*spaceX, self.bounds.size.height - 60);
   // self.bgScrollView.contentOffset = CGPointMake((_arrayX.count)*50 - CGRectGetWidth(self.bounds), 0);
    //获取拐点
    [self getInflectionPointWithArrayX:nil ArrayY:_arrayY color:0x4162FF];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        labelArray = [NSMutableArray arrayWithCapacity:0];
        btnArray = [NSMutableArray arrayWithCapacity:0];
        pointArray  = [NSMutableArray arrayWithCapacity:0];
        xLabelArray = [NSMutableArray arrayWithCapacity:0];
        //maxValue = 100;
        [self addSubview:self.bgScrollView];
        [self initDescribleLables];
    }
    return self;
}

//显示lable
-(void)initDescribleLables{

    //单位
    unitLabel = [[UILabel alloc]init];
    unitLabel.textColor = rgba(153, 153, 153, 1);
    unitLabel.textAlignment = NSTextAlignmentCenter;
    unitLabel.font = RFontWithSize(11);
    [self addSubview:unitLabel];
   
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(33);
        make.height.equalTo(@15.0f);
        make.left.equalTo(self.mas_left).with.offset(0.0f);
        make.right.equalTo(self.bgScrollView.mas_left).with.offset(5.0f);
    }];
}

//灰色背景线条 + 纵坐标
-(void)initBaseLineViewWithArrayY:(NSMutableArray *)array{
    
    
    for (int i = 0; i < baseLineCount; i++) {
        
        //灰色背景线条
        UIImageView * lineView = [[UIImageView alloc]init];
        lineView.backgroundColor = rgba(229, 229, 229, 1);
        [self.bgScrollView addSubview:lineView];
        
        if (i == baseLineCount -1) {
            lastLineView = lineView;
        }
        
        //纵坐标
        UILabel * labelY = [[UILabel alloc]init];
        labelY.textAlignment = NSTextAlignmentCenter;
        labelY.textColor = rgba(153, 153, 153, 1);
        labelY.font = MFontWithSize(10);
        //y坐标间隔
        double interval = maxValue/(baseLineCount -1)/100;
        labelY.text = [NSString stringWithFormat:@"%.f",ceil((baseLineCount - i -1)*interval*1.00)];
        [self addSubview:labelY];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.bgScrollView.mas_top).with.offset(8+i*space);
            make.left.equalTo(self.bgScrollView.mas_left).with.offset(0.0f);
            make.right.equalTo(self.mas_right).with.offset(22.0f);
            make.height.equalTo(@0.5f);
        }];
        
        [labelY mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.mas_left).with.offset(0.0f);
            make.right.equalTo(self.bgScrollView.mas_left).with.offset(5.0f);
            make.centerY.equalTo(lineView);
            make.height.equalTo(@17.0f);
            
        }];
    }
    
    if (moveLine.superview) {
        [moveLine removeFromSuperview];
    }
    
    moveLine = [[UIView alloc] initWithFrame:CGRectMake(50, 8, 1, (baseLineCount - 1)*space)];
    moveLine.hidden = YES;
    moveLine.backgroundColor = rgba(0, 0, 0, 0.15);
    [self.bgScrollView addSubview:moveLine];
}

//横坐标
-(void)initXLineWithArrayX:(NSMutableArray *)array{
    
    //横坐标
    for (int i = 0; i < array.count; i++) {
        
        UILabel * labelX = [[UILabel alloc]init];
        labelX.textAlignment = NSTextAlignmentCenter;
        labelX.textColor = rgba(102, 102, 102, 1);
        labelX.font = MFontWithSize(11);
        labelX.text = [NSString stringWithFormat:@"%@",array[i]];
        [self.bgScrollView addSubview:labelX];
        UIView *seperator = [[UIView alloc] initWithFrame:CGRectMake(i * (spaceX), (baseLineCount - 1)*space + 8, 1, 7)];
        seperator.backgroundColor = [UIColor blackColor];
        [self.bgScrollView addSubview:seperator];
        [labelX mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.bgScrollView.mas_left).with.offset(i * (spaceX));
            make.top.equalTo(lastLineView.mas_bottom).with.offset(5.0f);
            make.width.mas_equalTo(spaceX);
            make.height.equalTo(@17.0);
        }];
        [xLabelArray addObject:labelX];
    }
    
}

//获取拐点--注册
-(void)getInflectionPointWithArrayX:(NSMutableArray *)xArray ArrayY:(NSMutableArray *)yArray color:(long)color{
    
    [self layoutIfNeeded];
    NSMutableArray * midPointArray = [NSMutableArray arrayWithCapacity:0];
    for (UILabel * label in xLabelArray) {
        
        NSValue *point = [NSValue valueWithCGPoint:CGPointMake(label.center.x, 0)];
        [midPointArray addObject:point];
    }
    for (int i = 0; i < yArray.count; i++) {
        
        CGFloat currentData = [yArray[i] floatValue];
        CGFloat possionY = (230/maxValue)*(maxValue - currentData * 100) + 8;
        NSValue * point = midPointArray[i];
        CGPoint  OriginPoint = point.CGPointValue;
        OriginPoint.y = possionY;
        NSValue * newPoint = [NSValue valueWithCGPoint:OriginPoint];
        [pointArray addObject:newPoint];
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 32, (baseLineCount - 1)*space)];
        bgView.backgroundColor = rgba(244, 244, 244, 1);
        bgView.x = OriginPoint.x - 16;
        LTEllipseView *ellipse = [[LTEllipseView alloc] initWithFrame:CGRectMake(0, possionY, 32, bgView.height - possionY)];
        [bgView addSubview:ellipse];
        [self.bgScrollView addSubview:bgView];
        [bgView setCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:8];
        
    }
    
//    //曲线
//    //起点往前偏移
    CGPoint  originP = [[pointArray objectAtIndex:0] CGPointValue];
    CGPoint p1 = CGPointMake(originP.x - 27.5, originP.y);
    NSMutableArray * newPointArray = [NSMutableArray arrayWithArray:pointArray];
    NSValue *  point = [NSValue valueWithCGPoint:p1];
    [newPointArray insertObject:point atIndex:0];
    
    for (int i = 0;i<newPointArray.count;i++ ) {
        if (i != 0) {
            
            CGPoint nowPoint = [[newPointArray objectAtIndex:i] CGPointValue];
            if (i == newPointArray.count-1) {
               // [beizer moveToPoint:nowPoint];//添加连线
                lastPoint = nowPoint;
            }
        }
    }
    
    //添加点击btn
    for (UIButton * btn in btnArray) {
        [self.bgScrollView addSubview:btn];
    }
    
}

-(void)clickButtonAction:(UIButton *)sender{
    
    
    for (UIButton*btn in btnArray) {
        if (sender.tag == btn.tag) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
    
    [self showDeslLabel:sender];
}

-(void)showDeslLabel:(UIButton *)sender{
    
    for (int i = 0; i < labelArray.count; i++) {
        
        UIButton * btn = labelArray[i];
        
        if (![self.bgScrollView.subviews containsObject:btn]) {
            
            [self.bgScrollView addSubview:btn];
        }
        
        if (sender.tag + 1000 == btn.tag) {
            btn.hidden = NO;
        }else{
            btn.hidden = YES;
        }
    }
    
}

-(void)switchSelectTypeAction{
    
   
}

-(UIScrollView *)bgScrollView{
    
    if (!_bgScrollView) {
        _bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(48, 60, self.bounds.size.width - 15 - 48, self.bounds.size.height - 60)];
        _bgScrollView.backgroundColor = [UIColor redColor];
        _bgScrollView.showsVerticalScrollIndicator = NO;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
        _bgScrollView.backgroundColor = [UIColor whiteColor];
        _bgScrollView.contentSize = CGSizeMake((self.bounds.size.width - 15) * 3, self.bounds.size.height - 60);
        _bgScrollView.layer.cornerRadius = 6;
        _bgScrollView.bounces = NO;
      
    }
    return _bgScrollView;
}

@end
